class marinelistingsTagLib {

    static namespace = "wa"

    def ifDevEnv = { attrs, body ->
        def map = grailsApplication.metadata
        String env = map[grailsApplication.ENVIRONMENT]
        if (env == grailsApplication.ENV_DEVELOPMENT) {
            out << body()
        }
    }

    // if pagination...
    def ifPaginate = { attrs, body ->
        def count = attrs.count
        if (params.max.toInteger() && params.max.toInteger() < count) {
            out << body()
        }
    }
    
    // if user logged in...    
    def ifUser = { attrs, body ->
        boolean valid = session.userId != null
        if (attrs.not) {
            valid = !valid
        }
        if (valid) {
            out << body()
        }
    }
    
    // if user same as logged in user...
    def ifSameUser = { attrs, body ->
        boolean valid = session.userId && session.userId == attrs.id
        if (attrs.not) {
            valid = !valid
        }
        if (valid) {
            out << body()
        }
    }
    
    // if logged in user owns this item...
    def ifCurrentUserOwns = { attrs, body ->
        boolean valid = session.userId && session.userId == attrs.id
        if (attrs.not) {
            valid = !valid
        }
        if (valid) {
            out << body()
        }
    }

    // if logged in user has albums...
    def ifUserHasAlbums = { attrs, body ->
        boolean valid = session.albumsCount && session.albumsCount > 0
        if (attrs.not) {
            valid = !valid
        }
        if (valid) {
            out << body()
        }
    }
    
    def userLink = { attrs ->
		def user = User.get(session.userId)
        attrs.controller = 'user'
        attrs.action = 'show'
        attrs.id = user.id
        out << "<a href=\"${createLink(attrs).encodeAsHTML()}\""
        // process remaining attributes
        attrs.each { key, value ->
            out << " $key=\"$value\""
        }
        out << '>'
        out << user.name.encodeAsHTML()
        out << '</a>'
    }
    
    def pictureAnchor = { attrs, body ->
        def picture = attrs.remove('picture')
        def size = attrs.remove('size')
        out << "<a href=\"${createPictureLink(picture.id, size).encodeAsHTML()}\""
        attrs.each { key, value ->
            out << " $key=\"$value\""
        }
        out << '>'
        out << body()
        out << '</a>'
    }
  
    def pictureImg = { attrs ->
        def picture = attrs.remove('picture')
        def size = attrs.remove('size')
        out << createPictureImage(picture, size, attrs)
    }
  
    def pictureLightboxAnchor = { attrs ->
        def picture = attrs.remove('picture')
        def size = attrs.remove('size')
        def lightboxSize = attrs.remove('lightboxSize')
        def caption = picture.caption
        if (!caption) {
            caption = '...'
        }
        caption = caption.encodeAsHTML()
        StringBuilder sb = new StringBuilder(40)
        sb.append("<a href=\"${createPictureLink(picture.id, Image.Original).encodeAsHTML()}\"")
        attrs.each { key, value ->
            sb.append(" $key=\"$value\"")
        }
        sb.append('>')
        sb.append(caption.replaceAll(/'/, '&#39;'))
        sb.append('</a>')
        def link = sb.toString().encodeAsHTML()
        out << "<a href=\"${createPictureLink(picture.id, lightboxSize).encodeAsHTML()}\" rel=\"lightbox[list]\" title=\"${link}\">"
        out << createPictureImage(picture, size, null)
        out << '</a>'
    }
  
    private def createPictureImage(picture, size, attrs) {
        Integer width = 0
        Integer height = 0
        switch (size) {
            case Image.Original:
                width = picture.width
                height = picture.height
                break
            default:
                width = Image.Widths[size]
                height = Image.Widths[size]
                break
        } 
        def caption = picture.caption
        if (!caption) {
            caption = '...'
        }
        def alt = attrs?.remove('alt')
        if (!alt) {
            alt = caption
        }
        alt = alt.encodeAsHTML()
        def title = attrs?.remove('title')
        if (!title) {
            title = caption
        }
        title = title.encodeAsHTML()
        StringBuilder sb = new StringBuilder(40)
        sb.append("<img src=\"${createPictureLink(picture.id, size).encodeAsHTML()}\" alt=\"$alt\" title=\"$title\" width=\"$width\" height=\"$height\"")
        if (attrs) {
            attrs.each { key, value ->
                sb.append(" $key=\"$value\"")
            }
        }
        sb.append(' />')
        sb.toString()
    }
    
    private def createPictureLink(id, size) {
        def params = [ size: size, filename: Image.filename(id, size) ]
        createLink(url: [ controller: 'picture', action: 'view', id: id, params: params ])
    }
}